package com.ruoyi.ring.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ring.domain.RingRelaxInfo;
import com.ruoyi.ring.domain.vo.BreathingPatternVO;
import com.ruoyi.ring.mapper.RingRelaxInfoMapper;
import com.ruoyi.ring.service.IRingRelaxInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 减压助眠信息Service业务层处理
 * 
 * @author liuwp
 * @date 2025-10-22
 */
@Service
public class RingRelaxInfoServiceImpl implements IRingRelaxInfoService 
{
    @Autowired
    private RingRelaxInfoMapper ringRelaxInfoMapper;

    /**
     * 查询减压助眠信息
     * 
     * @param id 减压助眠信息主键
     * @return 减压助眠信息
     */
    @Override
    public RingRelaxInfo selectRingRelaxInfoById(Long id)
    {
        return ringRelaxInfoMapper.selectRingRelaxInfoById(id);
    }

    /**
     * 查询减压助眠信息列表
     * 
     * @param ringRelaxInfo 减压助眠信息
     * @return 减压助眠信息
     */
    @Override
    public List<RingRelaxInfo> selectRingRelaxInfoList(RingRelaxInfo ringRelaxInfo)
    {
        return ringRelaxInfoMapper.selectRingRelaxInfoList(ringRelaxInfo);
    }

    /**
     * 根据业务主键查询一条记录：减压助眠信息
     *
     * @param ringRelaxInfo 减压助眠信息
     * @return 减压助眠信息
     */
    @Override
    public RingRelaxInfo selectOneRingRelaxInfo(RingRelaxInfo ringRelaxInfo)
    {
        return ringRelaxInfoMapper.selectOneRingRelaxInfo(ringRelaxInfo);
    }

    /**
     * 新增减压助眠信息
     * 
     * @param ringRelaxInfo 减压助眠信息
     * @return 结果
     */
    @Override
    public int insertRingRelaxInfo(RingRelaxInfo ringRelaxInfo)
    {
        ringRelaxInfo.setCreateTime(DateUtils.getNowDate());
        return ringRelaxInfoMapper.insertRingRelaxInfo(ringRelaxInfo);
    }

    /**
     * 修改减压助眠信息
     * 
     * @param ringRelaxInfo 减压助眠信息
     * @return 结果
     */
    @Override
    public int updateRingRelaxInfo(RingRelaxInfo ringRelaxInfo)
    {
        ringRelaxInfo.setUpdateTime(DateUtils.getNowDate());
        return ringRelaxInfoMapper.updateRingRelaxInfo(ringRelaxInfo);
    }

    @Override
    public int updateRingRelaxInfoCondition(RingRelaxInfo ringRelaxInfo, RingRelaxInfo ringRelaxInfoCondition)
    {
                ringRelaxInfo.setUpdateTime(DateUtils.getNowDate());
        return ringRelaxInfoMapper.updateRingRelaxInfoCondition(ringRelaxInfo, ringRelaxInfoCondition);
    }

    /**
     * 批量删除减压助眠信息
     * 
     * @param ids 需要删除的减压助眠信息主键
     * @return 结果
     */
    @Override
    public int deleteRingRelaxInfoByIds(Long[] ids)
    {
        return ringRelaxInfoMapper.deleteRingRelaxInfoByIds(ids);
    }

    /**
     * 删除减压助眠信息信息
     * 
     * @param id 减压助眠信息主键
     * @return 结果
     */
    @Override
    public int deleteRingRelaxInfoById(Long id)
    {
        return ringRelaxInfoMapper.deleteRingRelaxInfoById(id);
    }

    /**
     * 条件删除减压助眠信息
     *
     * @param ringRelaxInfo 减压助眠信息
     * @return 结果
     */
    @Override
    public int deleteRingRelaxInfoByCondition(RingRelaxInfo ringRelaxInfo)
    {
        return ringRelaxInfoMapper.deleteRingRelaxInfoByCondition(ringRelaxInfo);
    }

    /**
     * 减压
     */
    @Override
    public List<BreathingPatternVO> stressRelief(int totalSeconds) {
        // 创建存储呼吸状态开始时间的列表
        List<BreathingPatternVO> breathingPatternVOS = new ArrayList<>();
        BreathingPatternVO breathingPatternVO = new BreathingPatternVO();
        // 呼吸周期：4秒吸气 + 4秒呼气 = 8秒
        int cycleDuration = 8;
        // 计算完整周期的数量（确保最后以呼气结束）
        int fullCycles = totalSeconds / cycleDuration;
        // 计算可完成的总秒数（完整周期的总时间）
        int completedSeconds = fullCycles * cycleDuration;
        // 当前秒数计数器
        int currentSecond = 1; // 从第1秒开始
        // 先添加初始状态：第1秒开始吸气
        breathingPatternVO.setSeconds(currentSecond);
        breathingPatternVO.setValue("吸气");
        breathingPatternVOS.add(breathingPatternVO);
        // 呼吸模式：4秒吸气，4秒呼气，循环往复
        while (true) {
            // 吸气持续4秒，之后开始呼气
            int nextExhale = currentSecond + 4;
            breathingPatternVO = new BreathingPatternVO();
            breathingPatternVO.setSeconds(nextExhale);
            breathingPatternVO.setValue("呼气");
            breathingPatternVOS.add(breathingPatternVO);
            currentSecond = nextExhale;
            // 呼气持续4秒，之后开始吸气
            int nextInhale = currentSecond + 4;
            if (nextInhale > completedSeconds) {
                // 如果下一个吸气时间超过总时长，当前呼气会持续到结束
                break;
            }
            breathingPatternVO = new BreathingPatternVO();
            breathingPatternVO.setSeconds(nextInhale);
            breathingPatternVO.setValue("吸气");
            breathingPatternVOS.add(breathingPatternVO);
            currentSecond = nextInhale;
        }
        return breathingPatternVOS;
    }

    /**
     * 助眠
     */
    @Override
    public  List<BreathingPatternVO> stressAid(int totalSeconds) {
        // 创建存储呼吸状态开始时间的列表
        List<BreathingPatternVO> breathingPatternVOS = new ArrayList<>();
        BreathingPatternVO breathingPatternVO = new BreathingPatternVO();
        // 呼吸阶段时长：4秒吸气 + 7秒屏气 + 8秒呼气
        int cycleDuration = 19;
        // 计算完整周期的数量（确保最后以呼气结束）
        int fullCycles = totalSeconds / cycleDuration;
        // 计算可完成的总秒数（完整周期的总时间）
        int completedSeconds = fullCycles * cycleDuration;
        // 当前秒数计数器，从第1秒开始
        int currentSecond = 1;
        // 先添加初始状态：第1秒开始吸气
        breathingPatternVO.setSeconds(currentSecond);
        breathingPatternVO.setValue("吸气");
        breathingPatternVOS.add(breathingPatternVO);
        // 循环生成呼吸周期
        while (true) {
            // 计算屏气开始时间（吸气结束后）
            int holdStart = currentSecond + 4;
            // 检查呼气开始时间是否超过总时长
            breathingPatternVO = new BreathingPatternVO();
            breathingPatternVO.setSeconds(holdStart);
            breathingPatternVO.setValue("屏住呼吸");
            breathingPatternVOS.add(breathingPatternVO);
            // 计算呼气开始时间（屏气结束后）
            int exhaleStart = holdStart + 7;
            // 添加呼气开始时间
            breathingPatternVO = new BreathingPatternVO();
            breathingPatternVO.setSeconds(exhaleStart);
            breathingPatternVO.setValue("呼气");
            breathingPatternVOS.add(breathingPatternVO);
            // 计算下一次吸气开始时间（当前呼气结束后）
            int nextInhale = exhaleStart + 8;
            // 检查下一次吸气是否超过总时长
            if (nextInhale > completedSeconds) {
                break;
            }
            // 添加下一次吸气开始时间
            breathingPatternVO = new BreathingPatternVO();
            breathingPatternVO.setSeconds(nextInhale);
            breathingPatternVO.setValue("吸气");
            breathingPatternVOS.add(breathingPatternVO);
            currentSecond = nextInhale;
        }
        return breathingPatternVOS;
    }
}
